﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe
{
    public partial class FrmNhapKho : Form
    {
        private NguyenLieuBL nguyenlieuBL = new NguyenLieuBL();
        private NhapKhoBL nhapkhoBL = new NhapKhoBL();
        private NhaCungCapBL nhaccBL = new NhaCungCapBL();
        private NhanVienBL nhanvienBL = new NhanVienBL();
        private CT_NhapKhoBL ctnhapkhoBL = new CT_NhapKhoBL();
        private DonViTinhBL dvtBL = new DonViTinhBL();
        private float tongtien;
        private int manl;
        public FrmNhapKho()
        {
            InitializeComponent();
            cbNhanVien.DataSource = nhanvienBL.LayDanhSachNhanVien();
            cbNhanVien.DisplayMember = "HoTenNV";
            cbNhanVien.ValueMember = "CMND_NV";
            cbNhaCC.DataSource = nhaccBL.LayDanhSachNCC();
            cbNhaCC.DisplayMember = "TenNCC";
            cbNhaCC.ValueMember = "MaNCC";
            cbNL.DataSource = nguyenlieuBL.DSNguyenLieu();
            cbNL.DisplayMember = "TenNL";
            cbNL.ValueMember = "MaNL";
            cbDVT.DataSource = dvtBL.LayDanhSachDVT();
            cbDVT.DisplayMember = "TenDVT";
            cbDVT.ValueMember = "MaDVT";
        }

        private void FrmNhapKho_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = nhapkhoBL.DSPhieuNhap() ;
            AnHienButton(false);
            btnXoa.Enabled = false;
        }
        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            //btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;
            btCongThuc.Enabled = flag;

            cbNhanVien.Enabled = flag;
            cbNhaCC.Enabled = flag;
            dataGridView1.Enabled = !flag;
        }
        

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                cbNhanVien.Text = dataGridView1.Rows[row].Cells["HoTenNV"].Value.ToString();
                dtNgayNhap.Text = dataGridView1.Rows[row].Cells["NgayNhap"].Value.ToString();
                tbMaNhap.Text = dataGridView1.Rows[row].Cells["MaNhap"].Value.ToString();
                cbNhaCC.Text = dataGridView1.Rows[row].Cells["TenNCC"].Value.ToString();
            }
            catch
            {

            }
        }

        private void btCongThuc_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            dataGridView2.DataSource = ctnhapkhoBL.DSNhapKhoNguyenLieu(int.Parse(tbMaNhap.Text));
            AnHienButtonCt(false);
            btnHuy.Visible = false; 
            tongtien = ctnhapkhoBL.TongTien(int.Parse(tbMaNhap.Text));
            tbTongTien.Text = tongtien.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            int manhap = nhapkhoBL.FindMa();
            tbMaNhap.Text = manhap.ToString();
            dtNgayNhap.Value = DateTime.Now;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Phiếu Nhập Ngày [" + dtNgayNhap.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (nhapkhoBL.XoaNK(tbMaNhap.Text))
                    FrmNhapKho_Load(sender, e);
        }
        private NhapKhoBO LayThongTinMon()
        {
            NhapKhoBO nhapkhoBO = new NhapKhoBO();
            nhapkhoBO.MaNhap = int.Parse(tbMaNhap.Text);
            nhapkhoBO.CMND_NV = cbNhanVien.SelectedValue.ToString();
            nhapkhoBO.MaNCC = int.Parse(cbNhaCC.SelectedValue.ToString());
            nhapkhoBO.NgayNhap = Convert.ToDateTime(dtNgayNhap.Text);
            nhapkhoBO.TongTien = float.Parse(tbTongTien.Text);
            return nhapkhoBO;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                NhapKhoBO nhapkhoBO = LayThongTinMon();
                if (nhapkhoBL.CheckPhieuNhap(nhapkhoBO.MaNhap) && ctnhapkhoBL.CheckCTNhapKho(int.Parse(tbMaNhap.Text)))
                {
                    if (nhapkhoBL.SuaNK(nhapkhoBO))
                    {
                        FrmNhapKho_Load(sender, e);
                    }
                }
                else if (ctnhapkhoBL.CheckCTNhapKho(int.Parse(tbMaNhap.Text)))
                {
                    if (nhapkhoBL.ThemNK(nhapkhoBO))
                        FrmNhapKho_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }


        private void AnHienButtonCt(bool flag)
        {
            btThemCT.Enabled = !flag;
            btSuaCT.Enabled = !flag;
            btXoaCT.Enabled = !flag;
            btLuuCT.Enabled = flag;
            btHuyCT.Enabled = flag;
            
            cbNL.Enabled = flag;
            tbSoLuong.Enabled = flag;
            tbDonGia.Enabled = flag;
            cbDVT.Enabled = flag;
            dataGridView2.Enabled = !flag;
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbSoLuong.Text = dataGridView2.Rows[row].Cells["SoLuong"].Value.ToString();
                tbDonGia.Text = dataGridView2.Rows[row].Cells["DonGia"].Value.ToString();
                cbNL.Text = dataGridView2.Rows[row].Cells["TenNL"].Value.ToString();
                cbDVT.Text = dataGridView2.Rows[row].Cells["TenDVT"].Value.ToString();
                
            }
            catch
            {

            }
        }

        private float tinhthanhtien()
        {
            int soluong = int.Parse(tbSoLuong.Text);
            int dongia = int.Parse(tbDonGia.Text);
            float thanhtien = soluong * dongia;
            return thanhtien;
        }
       

        private void btThemCT_Click(object sender, EventArgs e)
        {
            AnHienButtonCt(true);
            tbSoLuong.Text = "";
            tbDonGia.Text = "";
            manl = 0;
        }

        private void btSuaCT_Click(object sender, EventArgs e)
        {
            AnHienButtonCt(true);
            manl = int.Parse(cbNL.SelectedValue.ToString());
        }

        private void btXoaCT_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Bỏ Nguyên Liệu [" + cbNL.Text + "]  Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (ctnhapkhoBL.XoaCTNhapKho(cbNL.SelectedValue.ToString(), tbMaNhap.Text))
                {
                    dataGridView2.DataSource = ctnhapkhoBL.DSNhapKhoNguyenLieu(int.Parse(tbMaNhap.Text));
                    AnHienButtonCt(false);
                }
        }

        private CT_NhapKhoBO LayThongTinCTNhapKho()
        {
            CT_NhapKhoBO ctnhapkhoBO = new CT_NhapKhoBO();
            ctnhapkhoBO.MaNhap = int.Parse(tbMaNhap.Text);
            ctnhapkhoBO.SoLuong = int.Parse(tbSoLuong.Text);
            ctnhapkhoBO.DonGia = int.Parse(tbDonGia.Text);
            ctnhapkhoBO.MaDVT = int.Parse(cbDVT.SelectedValue.ToString());
            ctnhapkhoBO.MaNL = int.Parse(cbNL.SelectedValue.ToString());
            ctnhapkhoBO.ThanhTien = tinhthanhtien();
            return ctnhapkhoBO;
        }

        public bool testNL(int value1, int value2)
        {
            if (value1 == value2)
                return true;
            else return false;
        }

        private void btLuuCT_Click(object sender, EventArgs e)
        {
            try
            {
                CT_NhapKhoBO ctnhapkhoBO = LayThongTinCTNhapKho();
                if (ctnhapkhoBL.CheckCTNhapKho(ctnhapkhoBO.MaNhap))
                {
                    if (manl != 0)
                    {
                        if (testNL(manl, int.Parse(cbNL.SelectedValue.ToString())))
                        {
                            if (ctnhapkhoBL.SuaCTNKNL(ctnhapkhoBO))
                            {
                                dataGridView2.DataSource = ctnhapkhoBL.DSNhapKhoNguyenLieu(int.Parse(tbMaNhap.Text));
                                AnHienButtonCt(false);
                                tongtien = ctnhapkhoBL.TongTien(int.Parse(tbMaNhap.Text));
                                tbTongTien.Text = tongtien.ToString();
                            }
                        }
                        else
                        {
                            if (ctnhapkhoBL.SuaCTNhapKho(ctnhapkhoBO))
                            {
                                dataGridView2.DataSource = ctnhapkhoBL.DSNhapKhoNguyenLieu(int.Parse(tbMaNhap.Text));
                                AnHienButtonCt(false);
                                tongtien = ctnhapkhoBL.TongTien(int.Parse(tbMaNhap.Text));
                                tbTongTien.Text = tongtien.ToString();
                            }

                        }
                    }
                    else
                    {
                        if (ctnhapkhoBL.ThemCTNhapKho(ctnhapkhoBO))
                        {
                            dataGridView2.DataSource = ctnhapkhoBL.DSNhapKhoNguyenLieu(int.Parse(tbMaNhap.Text));
                            AnHienButtonCt(false);
                            tongtien = ctnhapkhoBL.TongTien(int.Parse(tbMaNhap.Text));
                            tbTongTien.Text = tongtien.ToString();
                        }
                    }
                }
                   
                else
                {
                    if (ctnhapkhoBL.ThemCTNhapKho(ctnhapkhoBO))
                    {
                        dataGridView2.DataSource = ctnhapkhoBL.DSNhapKhoNguyenLieu(int.Parse(tbMaNhap.Text));
                        AnHienButtonCt(false);
                        tongtien = ctnhapkhoBL.TongTien(int.Parse(tbMaNhap.Text)); 
                        tbTongTien.Text = tongtien.ToString();
                    }
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btHuyCT_Click(object sender, EventArgs e)
        {
            AnHienButtonCt(false);
        }

        private void btOK_Click(object sender, EventArgs e)
        {            
            panel1.Visible = false;
            btnHuy.Visible = true;
        }
    }
}
